﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "预约挂号管理";
    Layout = "~/Views/Shared/_Form.cshtml";
    //开启治疗项目组合
    var zlxmzhconf = SysConfigReader.String("EnableZlxmGroup");
}
<ul id="myTab" class="nav nav-tabs">
    <li class="active"><a href="#BookTab" data-toggle="tab">预约列表</a></li>
    <li><a href="#onlineBook" data-toggle="tab">在线预约</a></li>
</ul>
<div class="tab-content" style="margin-top:1%">
    <div class="tab-pane fade in active" id="BookTab">
        <form name="form1">
            <div class="panel panel-default">
                <table class="form">
                    <tr>

                        <th class="formTitle"> 关键字</th>
                        <td class="formValue">
                            <input type="text" class="form-control" id="keyValue" name="KeyValue" placeholder="姓名或证件号" />
                        </td>
                        <th class="formTitle">预约状态</th>
                        <td class="formValue">
                            <select id="sel_yyzt" class="form-control required" data-EnumType="EnumMzyyzt">
                                <option value="">请选择</option>
                            </select>
                        </td>
                        <th class="formTitle">预约来源</th>
                        <td class="formValue">
                            <select id="sel_yyly" class="form-control required" data-EnumType="EnumMzghly">
                                <option value="">请选择</option>
                            </select>
                        </td>
                        <th class="formTitle">预约日期：</th>
                        <td class="formValue" colspan="2">
                            <input id="txtyykssj" type="text" class="form-control input-wdatepicker formClearIgnore" style="width:42%; float:left;" value="@DateTime.Now.ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                            <span style="margin-left:2%;float:left;">—</span>
                            <input id="txtyyjssj" type="text" class="form-control input-wdatepicker formClearIgnore" style="width :41%;float:left;margin-left:2%;" value="@DateTime.Now.AddDays(3).ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                        </td>

                        <td class="formValue"></td>
                    </tr>
                    <tr>
                        <th class="formTitle">
                            <span class="required">*</span>科室：
                        </th>
                        <td class="formValue">
                            <input type="text" class="form-control" id="txtkschoose" />
                        </td>
                        <th class="formTitle"><span class="required">*</span>门诊类型：</th>
                        <td class="formValue formDdlSelectorTd">
                            <select id="sel_mzlx" class="form-control required" data-EnumType="EnumOutPatientType">
                                <option value="">请选择</option>
                            </select>

                        </td>
                        <td class="formValue" colspan="2">
                            <input type="button" id="btn_search" class="btn btn-primary btn-md" style="margin-left:10px;width:40px;" value="查询" />
                            <input type="button" id="btn_CancalYy" class="btn btn-primary btn-md" style="margin-left:10px;" value="取消预约" />
                        </td>
                    </tr>
                </table>
            </div>
            <div class="gridPanel">
                <table id="patGridList"></table>
                <div id="gridPager"></div>
            </div>
        </form>
    </div>
    <div class="tab-pane fade" id="onlineBook">
            <div class="panel panel-default">
                <table class="form">
                    <tr>
                        <th class="formTitle"><span class="required">*</span>病例号/姓名：</th>
                        <td class="formValue">
                            <input type="hidden" id="patid" />
                            <input type="text" class="form-control" id="blh" placeholder="最少1位字符" />
                        </td>
                        <th class="formTitle">
                            <input type="button" class="btn btn-default btn-md btn-default-color" id="btnsyy" value="查询" onclick="GetPatSerarchView($('#blh').val());" />
                        </th>
                        <th class="formTitle">姓名：</th>
                        <td class="formValue" style="width:80px;">
                            <label id="xm"></label>
                        </td>
                        <th class="formTitle">性别：</th>
                        <td class="formValue">
                            <label id="xb"></label>
                        </td>
                        <th class="formTitle">年龄：</th>
                        <td class="formValue">
                            <label id="nlshow"></label>
                        </td>
                        <td class="formTitle">卡号：</td>
                        <td class="formValue">
                            <label id="CardTypeName"></label>
                            <label id="kh"></label>
                        </td>
                    </tr>
                    <tr>
                        <td class="formTitle">
                            <label id="zjlx">证件号：</label>
                        </td>
                        <td class="formValue">
                            <label id="zjh"></label>
                            <input type="text" style="display:none;" id="hiddenzjlx" />
                        </td>
                        <th class="formTitle">初复诊：</th>
                        <td class="formValue">
                            <label id="txtcfz"></label>
                        </td>
                        <td class="formTitle"></td>
                        <td class="formValue"></td>
                        <td class="formTitle"></td>
                        <td class="formValue"></td>
                        <td class="formTitle"></td>
                        <td class="formValue"></td>
                    </tr>
                </table>
                <div class="panel-heading navb-bg">
                    排班筛选
                </div>
                <table class="form">
                    <tr>
                        <th class="formTitle">时间 ：</th>
                        <td class="formValue">
                            <input type="text" id="czrq" class="form-control input-wdatepicker formClearIgnore" style="height:20px;width:100px" value="@DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' });xzsj()" />
                        </td>
                        <th class="formTitle">门诊类型：</th>
                        <td class="formValue">
                            <select id="mjzbz" class="form-control required" data-EnumType="EnumOutPatientType">
                                <option value="">请选择</option>
                            </select>
                        </td>
                        <th class="formTitle">科室：</th>
                        <td class="formValue">
                            <input type="text" class="form-control" id="txtyykschoose" />
                        </td>

                        <th class="formTitle">排班医生：</th>
                        <td class="formValue"><input type="text" id="doctormc2" name="doctormc2" class="form-control form-an" /></td>
                        <td class="formValue" colspan="2">
                            <input type="button" id="btn_searchrl" class="btn btn-primary btn-md" style="margin-left:10px;width:40px;" value="查询" />
                            <input type="button" id="btn_yy" class="btn btn-primary btn-md" style="margin-left:10px;width:40px;" value="预约" />
                        </td>
                        <th class="formTitle"></th>
                    </tr>
                </table>
            </div>
            <div class="gridPanel">
                <table id="patGridList2"></table>
                @*<div id="gridPager2"></div>*@
            </div>
    </div>

</div>


<script>
    $(function () {
        $(window).resize(function () {
            initLayout("MyTabGrid");
        });

        InitBindSel();
        gridList();

    });
    function InitBindSel()
    {
        //科室
        $("#txtkschoose").newtouchBatchFloatingSelector({
            height: 200,
            width: 300,
            clickautotrigger: true,
            filter: function (keyword) {
                //遍历数据源，用keyword来筛选出结果
                var resultObjArr = new Array();
                $.each(top.window.clients.sysDepartList, function (idx, val) {
                    if ((val.py && val.py.toLowerCase().indexOf(keyword) >= 0)
                        || (val.Name && val.Name.indexOf(keyword) >= 0)
                        || keyword.trim() == "") {
                        resultObjArr.push(val);
                    }
                });
                return resultObjArr;

            },
            caption: "科室",
            colModel: [
                { label: '编号', name: 'Code', widthratio: 30 },
                { label: '名称', name: 'Name', widthratio: 50 },
                { label: '拼音', name: 'py', hidden: true }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#txtkschoose").attr("data-label", $thistr.find("td:eq(0)").html());
                $("#txtkschoose").val($thistr.find('td:eq(1)').html());
                $("#btn_search").trigger("click");
                return;
            },
        });
        //科室
        $("#txtyykschoose").newtouchBatchFloatingSelector({
            height: 200,
            width: 300,
            clickautotrigger: true,
            filter: function (keyword) {
                //遍历数据源，用keyword来筛选出结果
                var resultObjArr = new Array();
                $.each(top.window.clients.sysDepartList, function (idx, val) {
                    if ((val.py && val.py.toLowerCase().indexOf(keyword) >= 0)
                        || (val.Name && val.Name.indexOf(keyword) >= 0)
                        || keyword.trim() == "") {
                        resultObjArr.push(val);
                    }
                });
                return resultObjArr;

            },
            caption: "科室",
            colModel: [
                { label: '编号', name: 'Code', widthratio: 30 },
                { label: '名称', name: 'Name', widthratio: 50 },
                { label: '拼音', name: 'py', hidden: true }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#txtyykschoose").attr("data-label", $thistr.find("td:eq(0)").html());
                $("#txtyykschoose").val($thistr.find('td:eq(1)').html());
                $("#btn_searchrl").trigger("click");
                return;
            },
        });
        ///医生
        $("#doctormc2").newtouchFloatingSelector({
            height: 200,
            width: 480,
            clickautotrigger: true,
            filter: function (keyword) {
                //遍历数据源，用keyword来筛选出结果
                var resultObjArr = new Array();

                $.each(top.window.clients.sysStaffDutyList, function (idx, val) {
                    if (((val.StaffPY && val.StaffPY.toLowerCase().indexOf(keyword.toLowerCase()) >= 0)
                        || (val.StaffName && val.StaffName.indexOf(keyword.toLowerCase()) >= 0)
                        || keyword.trim() == "")
                        && val.DutyCode == "Doctor") {
                        resultObjArr.push(val);
                    }
                });
                return resultObjArr;

            },
            caption: "医生",
            colModel: [
                {
                    label: '工号', name: 'StaffGh', widthratio: 25
                },
                {
                    label: '名称', name: 'StaffName', widthratio: 25
                },
                { label: '拼音', name: 'StaffPY', widthratio: 25 }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#doctormc2").attr("data-label", $thistr.find("td:eq(0)").html());
                $("#doctormc2").val($thistr.find('td:eq(1)').html());
                $("#btn_searchrl").trigger("click");
                return;
            }
        });
    }

    function gridList()
    {
        var $gridList = $("#patGridList");
        $gridList.dataGrid({
            width:"100%",
            height: $(window).height() - 225,
            url: "/OutPatientManage/OutpatientReg/GetIsMzghBook",
            postData: getSearchPostData(),
            autowidth: true,
            caption: '预约列表',
            colModel: [
                { label: 'ghpbId', name: 'ScheduId', hidden: true, },
                {
                    label: '预约日期', name: 'OutDate', width: 60, align: 'left',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
                },
                { label: '姓名', name: 'xm', width: 50, align: 'left' },
                {
                    label: '性别', name: 'xb', width: 30, align: 'left', formatter: function (val) {
                        return val == "1" ? "男" : "女";
                    }
                },
                { label: '就诊序号', name: 'QueueNo', width: 50, align: 'left' },
                { label: '证件号', name: 'zjh', width: 100, align: 'left' },
                { label: 'ysgh', name: 'ysgh', width: 120, align: 'left', hidden: true },
                { label: '预约医生', name: 'ysxm', width: 55, align: 'left', },
                { label: 'BookId', name: 'BookId', hidden: true },
                { label: 'zlxm', name: 'zlxm', width: 100, align: 'left', hidden: true },
                { label: 'ghlx', name: 'ghlx', width: 120, align: 'left', hidden: true },
                {
                    label: '门诊类型', name: 'Regtype', width: 60, align: 'left', formatter: function (val) {
                        return $.enum.getDescByValue("EnumOutPatientType", val);
                    }
                },
                { label: 'Title', name: 'Title', width: 120, align: 'left', hidden: true },
                {
                    label: '预约来源', name: 'AppId', width: 60, align: 'left', formatter: function (val) {
                        return $.enum.getDescByName("EnumMzghly", val);
                    }
                },
                {
                    label: '预约状态', name: 'yyzt', width: 60, align: 'left', formatter: function (val) {
                        return $.enum.getDescByValue("EnumMzyyzt", val);
                    }
                },
                { label: 'ks', name: 'ks', hidden: true, width: 120, align: 'left', hidden: true },
                { label: '挂号科室', name: 'ksmc', width: 80, align: 'left' },
                { label: '挂号项目', name: 'sfxmmc', width: 120, align: 'left' },
                { label: '诊疗项目', name: 'zlxmmc', width: 120, align: 'left' },
                { label: '时间段', name: 'PeriodDesc', width: 80, align: 'left' },
                { label: '费用', name: 'RegFee', width: 40, align: 'left' }
            ],
            pager: "#gridPager",
            sortname: 'OutDate desc,ksmc,QueueNo',
            multiselect: false,
            viewrecords: true
        });
        $("#btn_search").click(function () {
            $gridList.jqGrid('setGridParam', {
                postData: getSearchPostData(),
            }).trigger('reloadGrid');
        });
        $("#sel_yyzt,#sel_yyly, #keyValue, #sel_mzlx").change(function () {
            $gridList.jqGrid('setGridParam', {
                postData: getSearchPostData(),
            }).trigger('reloadGrid');
        });

        var $gridList2 = $("#patGridList2");
        $gridList2.dataGrid({
            autowidth: false,
            height: $(window).height() - 225,
            url: "/OutpatientManage/OutpatientReg/GetNewRegSchedulebyGroupList",
            postData: getyySearchPostData(),
            caption: '排班列表',
            colModel: [
                { label: '排班描述', name: 'Title', width: 120 },
                {
                    label: '出诊日期', name: 'OutDate', width: 100, formatter: "date",
                    formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
                },
                {
                    label: '门诊类型', name: 'RegType', width: 80, formatter: function (val) {
                        return $.enum.getDescByValue("EnumOutPatientType", val);
                    }
                },
                { label: '挂号科室', name: 'ksmc', width: 105 },
                { label: '挂号项目', name: 'sfxmmc', width: 120 },
                { label: '诊疗项目组合', name: 'zlxmmc', width: 120 },
                { label: '医生', name: 'rymc', width: 70 },
                
                { label: '费用', name: 'RegFee', width: 60 },
                { label: '号源', name: 'TotalNum', width: 50 },
                {
                    label: '剩余号源', name: 'YYNum', width: 50, formatter: function (cellvalue, options, rowobject) {
                        return (rowobject.TotalNum - rowobject.YYNum)
                    }
                },
                {
                    label: '开放预约', name: 'IsBook', width: 60, formatter: function (val) {
                        return $.enum.getDescByValue("EnumMzpbBook", val);
                    }
                },
                { label: '时间段', name: 'PeriodDesc', width: 120 },
                { label: 'RegType', name: 'RegType', hidden: true },
                { label: 'ks', name: 'ks', hidden: true },
                { label: 'gh', name: 'gh', hidden: true },
                { label: 'ghlx', name: 'ghlx', hidden: true },
                { label: 'zlxm', name: 'zlxm', hidden: true },
                { label: 'ghpbId', name: 'ghpbId', hidden: true },
            ],
            //pager: "#gridPager2",
            //sortname: 'RegType desc,ksmc',
            viewrecords: true,
        });
        $("#btn_searchrl").click(function () {
            $gridList2.jqGrid('setGridParam', {
                postData: getyySearchPostData(),
            }).trigger('reloadGrid');
        });
        $("#btn_searchrl, #mjzbz").change(function () {
            $gridList2.jqGrid('setGridParam', {
                postData: getyySearchPostData(),
            }).trigger('reloadGrid');
        });
    }
    function getSearchPostData()
    {
        return {
            yykssj: $("#txtyykssj").val(), yyjssj: $("#txtyyjssj").val(), yyzt: $("#sel_yyzt").val(), ghly: $("#sel_yyly").val(),
            keyValue: $("#keyValue").val(), isfeegroup: '@zlxmzhconf', mzlx: $("#sel_mzlx").val(), ks: $("#txtkschoose").val()//attr("data-label")
        };
    }
    function getyySearchPostData()
    {
        return {
            pbks: $("#txtyykschoose").val(), mzlx: $("#mjzbz").val(), ys: $("#doctormc2").val(),
            OutDate: $("#czrq").val()
        };
    }


    //患者查询弹出
    function GetPatSerarchView(blh) {
        $.modalOpen({
            id: "patSearch",
            title: "患者查询",
            url: "/OutpatientManage/OutpatientAccounting/SysPatEntitiesblhView?t=" + Math.random() + "&blh=" + blh,
            width: "700px",
            height: "600px",
            callBack: function (iframeId) {
                top.frames[iframeId].PatDbGrid(); //在弹出窗口事件
            }
        });
    }
    var patModel = null;
    function GetQueryFphAjax(obj, funcSuccCallback) {
        newtouch_event_f4();
        $.najax({
            url: "/PatientManage/HospiterRes/GetOutpatientBasicInfo",
            data: obj,
            dataType: "json",
            async: false,
            success: function (rep) {
                if (rep.data && !!!rep.data.brxz) {
                    $.modalAlert("患者信息异常，缺少费用性质", 'warning');
                    return false;
                }
                if (rep.data && !!!rep.data.CardTypeName) {
                    $.modalAlert("患者卡类型异常", 'warning');
                    return false;
                }
                patModel = rep.data;
                $("#xb").html($.getGender(patModel.xb));
                $("#zjh").html(patModel.zjh);
                $("#xm").html(patModel.xm);
                $("#blh").val(patModel.blh);
                $("#nlshow").html(getAgeFromBirthTime({ begin: patModel.csny.replace('T', ' ') }).text);
                $("#kh").html(patModel.kh);
                $("#patid").val(patModel.patid);
                $("#CardTypeName").html('（' + patModel.CardTypeName + '）');
                if (patModel.fzbz === 0) {
                    $("#txtcfz").html("初诊");
                }
                else {
                    $("#txtcfz").html("复诊");
                }
            }
        });
    }
    //预约
    $("#btn_yy").click(function () {
        if (!patModel) {
            $.modalAlert("尚未选择患者", "warning");
            return;
        }
        var seleRow = check();
        if (!!!seleRow) {
            return;
        }
        $.najax({
            url: "/OutpatientManage/OutBook/PatBookGh",
            dataType: "json",
            loadingtext: "正在预约，请稍后…",
            data: { OutDate: seleRow.OutDate, cardNo: patModel.kh, ScheduId: seleRow.ghpbId, brxz: patModel.brxz },
            type: "POST",
            success: function (ajaxresp) {
                $.modalAlert("预约成功", "success");
                $("#btn_searchrl").trigger("click");
            }
        });
       
    });
    function check()
    {
        var seleRow = $("#patGridList2").jqGridRowValue();
        if (!!!seleRow.ghpbId)
        {
            $.modalAlert("尚未选择一条记录", "warning");
            return;
        }
        if (seleRow.TotalNum==='0') {
            $.modalAlert("该排班无号源", "warning");
            return;
        }
        if (seleRow.YYNum === '0') {
            $.modalAlert("该排班无号源", "warning");
            return;
        }
        if (seleRow.IsBook === '否') {
            $.modalAlert("该排班未开放预约", "warning");
            return;
        }
        if (seleRow.OutDate < $.getDate()) {
            $.modalAlert("只能预约当日之后的日期", "warning");
            return;
        }
        return seleRow;
    }


    //取消预约
    $("#btn_CancalYy").click(function () {
        var seleRow = $("#patGridList").jqGridRowValue();
        if (!!!seleRow.BookId) {
            $.modalAlert("尚未选择一条记录", "warning");
            return;
        }
        if (seleRow.AppId!="His")
        {
            $.modalAlert("非HIS端预约请至第三方预约平台取消", "warning");
            return;
        }
        $.najax({
            url: "/OutpatientManage/OutBook/CancalBook",
            dataType: "json",
            loadingtext: "正在预约，请稍后…",
            data: { BookId: seleRow.BookId },
            type: "POST",
            success: function (ajaxresp) {
                $.modalAlert("取消预约成功", "success");
                $("#btn_search").trigger("click");
            }
        });
     
    });

    function newtouch_event_f4() {
        patModel = null;
        $("#patid").val("");
        $("#blh").val("");
        $("#xm").html("");
        $("#xb").html("");
        $("#nlshow").html("");
        $("#CardTypeName").html('');
        $("#kh").html("");
        $("#zjh").html("");
        $("#txtcfz").html("");
    }

</script>